package com.controller.admin;

import com.controller.BaseController;
import com.entity.RechargeLog;
import com.github.pagehelper.PageInfo;
import com.service.imple.admin.RechargeLogServiceImple;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.enterprise.inject.New;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/***
 * 充值记录
 */

@Controller
@RequestMapping("admin/rechargeLog")
public class AdminRechargeLogController extends BaseController<RechargeLogServiceImple> {

    @RequestMapping("toToPu")
    public String toToPu(){
        return "center/admin/topUp/topUp";
    }
    @RequestMapping("ToPu")
    public String ToPu(@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum, ModelMap modelMap,String name){
        Map<String,Object> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("userName",name);
        PageInfo<RechargeLog> pageInfo = BaseService.selectByPage(map);
        modelMap.put("pageInfo",pageInfo);
        return "center/admin/topUp/topUpPage";
    }

    @RequestMapping("Recharge")
    @ResponseBody
    public Map<String,Object> recharge(Integer userId, Integer money){
        Map<String,Object> map = new HashMap<>();
        RechargeLog rechargeLog = new RechargeLog();
        rechargeLog.setUserId(userId);
        rechargeLog.setMoney(BigDecimal.valueOf(money));
        rechargeLog.setAddTime(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = sdf.format(new Date());
        rechargeLog.setRechargeSn(dateString);
        int count = BaseService.insert(rechargeLog);
        System.out.println(count);
        if (count>=1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

    @RequestMapping("ServletRecharge")
    public String ServletRecharge(@SessionAttribute("userId") Integer userId,ModelMap modelMap){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        List<RechargeLog> list = BaseService.select(map);
        modelMap.put("list",list);
        return"center/amount/log";
    }


}
